package com.miage.itrading.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

import com.miage.itrading.entites.Cotation;

public class ResultScreening extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Cotation> cotations;
	private int periode;
	ModeleDynamiqueObjet model;
	JTable cotsTbl;
	TableRowSorter<ModeleDynamiqueObjet> sorter;
	JLabel nbCot = new JLabel();

	public ResultScreening(ArrayList<Cotation> cotations,Component parent, int periode){

		switch (periode){
		case 0 : this.periode = 1;break;
		case 1 : this.periode = 5;break;
		case 2 : this.periode = 21;break;
		case 3 : this.periode = 253;break;
		}

		this.cotations = cotations;
		setTitle("Resultat du screening");
		setLocationRelativeTo(null);
		setSize(500,500);
		setResizable(false);
		setModal(true);
		add(cotations());

		refreshResults(cotations);
		setLocationRelativeTo(parent);
		setVisible(true);

	}

	private void refreshResults(ArrayList<Cotation> cots)
	{
		model.refreshData(cots);
		cotsTbl.setModel(model);
		cotsTbl.updateUI();
		nbCot.setText(cots.size()+" cotations trouvées");
	}

	private JPanel cotations()
	{
		JPanel cotation = new JPanel();

		cotation.setPreferredSize(new Dimension(500,500));
		if(cotations.size()>0)
		{

			model = new ModeleDynamiqueObjet(periode);
			cotsTbl = new JTable(model);
			// Trie
			sorter = new TableRowSorter<ModeleDynamiqueObjet>(model);	 
			cotsTbl.setRowSorter(sorter);
			sorter.setComparator(1, new VariationComparator());
			sorter.setComparator(2, new VariationComparator());
			sorter.setSortsOnUpdates(true); // Il faut retrier le tableau après une mise à jour des données dans le tableau

			cotation.add(cotsTbl.getTableHeader());
			cotation.add(new JScrollPane(cotsTbl));
		}
		return cotation;
	}

	public class ModeleDynamiqueObjet  extends AbstractTableModel{

		private static final long serialVersionUID = -3142968896510127064L;
		private final List<ModelOjectRow> donnees = new ArrayList<ModelOjectRow>();
		private int periode ;
		private final String[] entetes = {"Nom","Cours", "Var %"};

		public ModeleDynamiqueObjet (List<Cotation> cots, int periode)
		{
			super();
			this.periode = periode;
			refreshData(cots);
		}

		public ModeleDynamiqueObjet(int periode) {
			super();
			this.periode = periode;
		
		}

		public void refreshData(List<Cotation> cots)
		{
			donnees.clear();
			for (Cotation cotation : cots) {
				donnees.add(new ModelOjectRow(cotation,periode));
			}
		}

		public int getColumnCount() {
			return entetes.length;
		}

		public int getRowCount() {
			return donnees.size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			switch (columnIndex) {
			case 0:
				return donnees.get(rowIndex).getNom();
			case 1:
				return donnees.get(rowIndex).getCotation();
			case 2:
				return Float.valueOf(new DecimalFormat("0.00").format(donnees.get(rowIndex).getVar()).replace(",", "."));
			default:
				return null; // Ne devrait jamais arriver
			}
		}

		public String getColumnName(int columnIndex)
		{
			return entetes[columnIndex];
		}
	}

	/*
	 * TODO calculer les variations
	 */
	private class ModelOjectRow{
		Cotation cot;
		int periode ;
		
		public ModelOjectRow(Cotation c, int periode)
		{
			cot = c;
			this.periode = periode;
		}

		public String getNom()
		{
			return cot.getNom();
		}

		public Float getCotation()
		{
			return cot.getHistoriques().get(cot.getHistoriques().size() -1).getFermeture();
		}

		public Float getVar()
		{
			float val1 = cot.getHistoriques().get(cot.getHistoriques().size() -1).getFermeture();
			float val2 = cot.getHistoriques().get(cot.getHistoriques().size() -1 - periode).getFermeture();
			float var = ((val1 / val2) -1) * 100;
			return var;
		}
	}


	/**
	 * @author Jerem
	 *	Utile lorsque l'on affichera les caractère "%"
	 */
	public class VariationComparator implements Comparator<Float> {

		public int compare(Float a, Float b) {
			return a.compareTo(b);
		}

	}
}